package com.le.tester.imooc.ccmouse.recursion;

import com.le.tester.imooc.ccmouse.common.Node;

import java.util.Arrays;

/**
 * 描述：链表的反转(递归)
 */
public class LinkedListReverser {

    /**
     * reverses linked list
     *
     * @param head Node next
     * @return linked list reversed
     */
    public Node reverseLinkedList(Node head) {

        if (head == null || head.getNext() == null) {
            return head;
        }
        Node newHead = this.reverseLinkedList(head.getNext());
        head.getNext().setNext(head);
        head.setNext(null);
        return newHead;
    }

    public static void main(String[] args) {
        LinkedListCreator linkedListCreator = new LinkedListCreator();
        LinkedListReverser linkedListReverser = new LinkedListReverser();

//        Node.printLinkedList(linkedListReverser.reverseLinkedList(
//                linkedListCreator.createLinkedList(new ArrayList<>())));
//
//        Node.printLinkedList(linkedListReverser.reverseLinkedList(
//                linkedListCreator.createLinkedList(Collections.singletonList(1))));

        Node.printLinkedList(linkedListReverser.reverseLinkedList(
                linkedListCreator.createLinkedList(Arrays.asList(1, 2, 3, 4, 5))));
//        linkedListReverser.reverseLinkedList(
//                linkedListCreator.createLargeLinkedList(1000000));
//        System.out.println("done");
    }
}
